/** LoginPanel **/

package docoffice.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import docoffice.doss.Authentication;

/**
 * @author George Chi
 * @version $Revision: 1.0 $
 */
public class LoginPanel extends JPanel {
	/**
	 * Field pass.
	 * Field user.
	 * Field blank2.
	 * Field blank.
	 * Field loginTitle.
	 */
	private final JLabel loginTitle, blank, blank2, user, pass;

	/**
	 * Field passText.
	 * Field userText.
	 */
	private final JTextField userText, passText;

	/**
	 * Field login.
	 */
	private final JButton login;

	/**
	 * Field userType.
	 */
	private String userType;

	/**
	 * Constructor for LoginPanel.
	 */
	public LoginPanel() {
		loginTitle = new JLabel("Doctor's Office Support System");
		blank = new JLabel();
		blank2 = new JLabel();
		user = new JLabel("Username");
		pass = new JLabel("Password");
		login = new JButton("Login");
		
		userText = new JTextField(10);
		passText = new JPasswordField(10);
		
		final LoginListener listener = new LoginListener();
		login.addActionListener(listener);
		
		setLayout(new GridLayout(4, 2, 15, 10));
		add(loginTitle);
		add(blank);
		add(user);
		add(userText);
		add(pass);
		add(passText);
		add(blank2);
		add(login);

		setPreferredSize(new Dimension(450, 130));
		setBackground(Color.WHITE);
	}
	
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}
	
	/**
	 * @author George Chi
	 */
	private class LoginListener implements ActionListener {

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent e) {
			final String username = userText.getText();
			final String password = passText.getText();
			
			final Authentication newAuth = new Authentication();
			userType = newAuth.authenticate(username, password);
			
			System.out.println(userType);
			
			if(userType.equals("Doctor")) {
				System.out.println("Doctor login successful");
				removeAll();
				revalidate();
				repaint();
				add(new DoctorPanel());
			}
			else if(userType.equals("Nurse")) {
				System.out.println("Nurse login successful");
				removeAll();
				revalidate();
				repaint();
				add(new NursePanel());
			}
			else if(userType.equals("SysAdmin")) {
				System.out.println("SysAdmin login successful");
				removeAll();
				revalidate();
				repaint();
				add(new SysAdminPanel());
			}
			else if(userType.equals("Patient")) {
				System.out.println("Patient login successful");
				removeAll();
				revalidate();
				repaint();
				add(new PatientPanel());
				
			}
			else {
				System.out.println("Fail");
			}
		}

		/**
		 * Method toString.
		
		 * @return String */
		public String toString() {
			return "LoginListener [toString()=" + super.toString() + "]";
		}
	}
	
}